package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DAL.JDBC.DBConn;
import DAL.JDBC.GenreJDBC;

public class AddGenre extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private GenreJDBC genreJDBC;
	
    public void init()
    {
    	genreJDBC = new GenreJDBC(DBConn.getConnection());
    }
    
    public AddGenre() {
        super();
        // TODO Auto-generated constructor stub
    }
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject result = new JSONObject();
		
		
		String name = request.getParameter("name");
		try {
			if(genreJDBC.getGenreByName(name) == null)
			{
				genreJDBC.insert(name);
				result.put("result", true);
			}
			else
			{
				result.put("result", false);
				result.put("message", "Genre Exist");
			}
			
			PrintWriter pw = response.getWriter();
			pw.write(result.toString());
		} 
		
		catch (JSONException e) {
			e.printStackTrace();
		}
		
		
		
		
		
	}

}
